import AuthorDetails from '@site/src/components/AuthorDetails';

## Cohere

### Prerequisites

```bash
pip install cohere
```

- Cohere `api_key` (_**for non-cached examples**_)

### Setting up the Cohere Client

The constructor initializes the base class `LM` to support prompting requests to Cohere models. This requires the following parameters:



**Parameters:**
- `model` (_str_): Cohere pretrained models. Defaults to `command-xlarge-nightly`.
- `api_key` (_Optional[str]_, _optional_): API provider provider authentication token. Defaults to `None`. This then internally initializes the `cohere.Client`.
- `stop_sequences` (_List[str]_, _optional_): List of stopping tokens to end generation.
- `max_num_generations` _internally set_: Maximum number of completions generations by Cohere client. Defaults to 5.

Example of the Cohere constructor:

```python
class Cohere(LM):
    def __init__(
        self,
        model: str = "command-xlarge-nightly",
        api_key: Optional[str] = None,
        stop_sequences: List[str] = [],
    ):
```

### Under the Hood

#### `__call__(self, prompt: str, only_completed: bool = True, return_sorted: bool = False, **kwargs) -> List[Dict[str, Any]]`

**Parameters:**
- `prompt` (_str_): Prompt to send to Cohere.
- `only_completed` (_bool_, _optional_): Flag to return only completed responses and ignore completion due to length. Defaults to True.
- `return_sorted` (_bool_, _optional_): Flag to sort the completion choices using the returned averaged log-probabilities. Defaults to False.
- `**kwargs`: Additional keyword arguments for completion request.

**Returns:**
- `List[str]`: List of generated completions.

Internally, the method handles the specifics of preparing the request prompt and corresponding payload to obtain the response. 

The method calculates the number of iterations required to generate the specified number of completions `n` based on the `self.max_num_generations` that the Cohere model can produce in a single request. As it completes the iterations, it updates the official `num_generations` argument passed to the request payload and calls request with the updated arguments. 

This process iteratively constructs a `choices` list from which the generated completions are retrieved. 

If `return_sorted` is set and more than one generation is requested, the completions are sorted by their likelihood scores in descending order and returned as a list with the most likely completion appearing first.

### Using the Cohere client

```python
cohere = dsp.Cohere(model='command-xlarge-nightly')
```

### Sending Requests via Cohere Client

1) _**Recommended**_ Configure default LM using `dspy.configure`.

This allows you to define programs in DSPy and simply call modules on your input fields, having DSPy internally call the prompt on the configured LM.

```python
dspy.configure(lm=cohere)

#Example DSPy CoT QA program
qa = dspy.ChainOfThought('question -> answer')

response = qa(question="What is the capital of Paris?") #Prompted to cohere
print(response.answer)
```

2) Generate responses using the client directly.

```python
response = cohere(prompt='What is the capital of Paris?')
print(response)
```

***

<AuthorDetails name="Arnav Singhvi"/>